//
// Created by mac on 2022/3/14.
//
#include <iostream>
using namespace std;
struct ListNode {
    int val;
    ListNode *next;
    ListNode() : val(0), next(nullptr) {}
    ListNode(int x) : val(x), next(nullptr) {}
    ListNode(int x, ListNode *next) : val(x), next(next) {}
    };

ListNode* removeElements(ListNode* head, int val) {
    ListNode *dummy = new ListNode(-1);
    dummy->next = head;
    ListNode *currnode = head;
    ListNode *prev = dummy;
    while (currnode){
        if(currnode->val==val){
            prev->next = currnode->next;
            currnode->next = nullptr;
        } else{
            prev = currnode;
        }
        currnode = prev->next;
    }
    return dummy->next;
}